//
//  TTDetailFilterExistView.m
//  TaoTuo
//
//  Created by admin on 2019/12/10.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDetailFilterExistView.h"

static NSString *cellIdentifier = @"detail_filter_cell_id";

@interface TTDetailFilterExistView ()

@property (nonatomic, strong) TTDetailModel *model;

@end

@implementation TTDetailFilterExistView

- (instancetype)initWithFrame:(CGRect)frame model:(TTDetailModel *)model dataArray:(nonnull NSArray *)dataArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self setModel:model dataArray:dataArray];
        
    }
    return self;
}

- (void)setModel:(TTDetailModel *)model dataArray:(NSArray *)dataArray
{
    self.model = model;
    [self.dataArray addObjectsFromArray:dataArray];
    
    [self setupSubviews];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTDetailStockListModel *model = self.dataArray[indexPath.row];
    TTSpecificationViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.delegate = self;
    [cell setModel:model specificationCellType:CellTypeWithSpecificationChoose categoryName:self.model.CategoryName];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTDetailStockListModel *model = self.dataArray[indexPath.row];
    model.selected = !model.selected;
    if (model.selected) {
        if (![self.selectArray containsObject:model]) {
            [self.selectArray addObject:model];
        }
    } else {
        [self.selectArray removeObject:model];
    }
    
    [self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTDetailStockListModel *model = self.dataArray[indexPath.row];
    return model.h;
}


#pragma mark - MGSwipeTableCellDelegate
- (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction fromPoint:(CGPoint)point
{
    return NO;
}


- (void)setupSubviews
{
    CGFloat h = self.height;
    
    [self addSubview:self.headerView];
    
    self.tableView.frame = CGRectMake(0, self.headerView.bottom, self.width, h - self.headerView.height);
    [self addSubview:self.tableView];
    
    [self.tableView reloadData];
    
}



- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.emptyImageName = @"nodata";
        _tableView.emptyTitel = @"暂无数据";
        [_tableView registerClass:[TTSpecificationViewCell class] forCellReuseIdentifier:cellIdentifier];
    }
    return _tableView;
}

- (TTDetailFilterHeaderView *)headerView
{
    if (!_headerView) {
        @weakify(self)
        _headerView = [[TTDetailFilterHeaderView alloc] initWithFrame:CGRectZero model:self.model dataArray:self.dataArray canShowDatas:^(NSArray * _Nullable datas) {
            @strongify(self)
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:datas];
            [self.tableView reloadData];
            if (self.dataArray.count > 0) {
                [self.tableView scrollToRow:0 inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
            }
        }];
        
    }
    return _headerView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)selectArray
{
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}



@end
